#include "StdAfx.h"
#include "TestInfo.h"
#include "GroupInfo.h"

TestInfo::TestInfo(void)
:CurrentGroupId(0)
{
}

TestInfo::TestInfo( CMarkup& xml )
:CurrentGroupId(0)
{
	TestId = _ttoi(xml.GetAttrib(_T("TestId")));
	Description = xml.GetAttrib(_T("Description"));
	CLF = xml.GetAttrib(_T("CLF"));

	{
		int r = 0;
		int g = 0;
		int b = 0;

		CString strColor= xml.GetAttrib(_T("BackGroundColor"));
		if(!strColor.IsEmpty())
			sscanf(strColor,_T("RGB(%d,%d,%d)"),&r,&g,&b);
		BackGroundColor = RGB(r,g,b);
	}

	{
		int r = 0;
		int g = 0;
		int b = 0;

		CString strColor= xml.GetAttrib(_T("AdditionalStudentBackGroundColor"));
		if(!strColor.IsEmpty())
			sscanf(strColor,_T("RGB(%d,%d,%d)"),&r,&g,&b);
		AdditionalStudentBackGroundColor = RGB(r,g,b);
	}

	AudioSource = xml.GetAttrib(_T("AudioSource"));

	xml.IntoElem();

	while(xml.FindElem(_T("GroupInfo")))
	{
		GroupInfo* pGroupInfo = GroupInfo::Create(TestId,xml);
		if(pGroupInfo)
			vGroupInfo.push_back(pGroupInfo);
	}

	xml.OutOfElem();
}

BOOL TestInfo::Save( CMarkup& xml )
{
	xml.AddElem(_T("TestInfo"));
	xml.AddAttrib(_T("TestId"),TestId);
	xml.AddAttrib(_T("Description"),Description);
	xml.AddAttrib(_T("CLF"),CLF);
	{
		int r = GetRValue(BackGroundColor);
		int g = GetGValue(BackGroundColor);
		int b = GetBValue(BackGroundColor);
		CString strColor;
		strColor.Format("RGB(%d,%d,%d)",r,g,b);
		xml.AddAttrib(_T("BackGroundColor"),strColor);
	}
	{
		int r = GetRValue(AdditionalStudentBackGroundColor);
		int g = GetGValue(AdditionalStudentBackGroundColor);
		int b = GetBValue(AdditionalStudentBackGroundColor);
		CString strColor;
		strColor.Format("RGB(%d,%d,%d)",r,g,b);
		xml.AddAttrib(_T("AdditionalStudentBackGroundColor"),strColor);
	}
	xml.AddAttrib(_T("AudioSource"),AudioSource);

	xml.IntoElem();

	size_t size = vGroupInfo.size();
	for(size_t i=0; i<size; ++i)
		vGroupInfo[i]->Save(xml);

	xml.OutOfElem();

	return true;
}

TestInfo::~TestInfo(void)
{
	size_t size = vGroupInfo.size();
	for(size_t i=0; i<size; ++i)
		SAFE_DELETE(vGroupInfo[i]);
	vGroupInfo.clear();
}

const CString& TestInfo::GetDescription()
{
	return Description;
}

const CString& TestInfo::GetCLF()
{
	return CLF;
}

COLORREF TestInfo::GetBackGroundColor()
{
	return BackGroundColor;
}

COLORREF TestInfo::GetAdditionalStudentBackGroundColor()
{
	return AdditionalStudentBackGroundColor;
}

const CString& TestInfo::GetAudioSource()
{
	return AudioSource;
}

BOOL TestInfo::SetCLF( const CString& clf )
{
	CLF = clf;
	// TODO : if clf's parent directory is not exist , then create it
	return true;
}

BOOL TestInfo::SetAudioSource( const CString& audioSource )
{
	AudioSource = audioSource;
	// TODO : if audioSource is not valid directory , return false;
	return true;
}

GroupInfo* TestInfo::GetGroupInfo( UINT i )
{
	return (0 <= i && i< vGroupInfo.size()) ? vGroupInfo[i] : NULL;
}

size_t TestInfo::GetGroupSize()
{
	return vGroupInfo.size();
}

GroupInfo* TestInfo::GetCurrentGroupInfo()
{
	GroupInfo* ret = GetGroupInfo(CurrentGroupId);
	ASSERT(ret);
	return ret;
}

BOOL TestInfo::SetCurrentGroup( UINT i )
{
	if(0 <= i && vGroupInfo.size())
	{
		CurrentGroupId = i;
		return true;
	}
	return false;
}
UINT TestInfo::GetCurrentGroup()
{
	return CurrentGroupId;
}